package com.aaa.controller.front;

import com.aaa.entity.FrontUserinfo;
import com.aaa.entity.ShippingAddress;
import com.aaa.service.front.IFrontUserAddressService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/front")
@Controller
public class FrontUserAddressController {

    @Resource
    private IFrontUserAddressService frontUserAddressService;

//我的地址
  @RequestMapping("/userAddress")
     public String udai_address(Model model, Integer UserInfoId, HttpSession session){
      FrontUserinfo f=(FrontUserinfo) session.getAttribute("FrontUserinfo");
      UserInfoId=f.getUserInfoId();
      List<ShippingAddress> addressList=frontUserAddressService.listByAddress(UserInfoId);
      model.addAttribute("addressList",addressList);
//      return "/front/myaccount";
      return "/front/udai_address";
  }
 //添加地址
@RequestMapping("/userAddressAdd")
public String userAddressAdd(ShippingAddress shippingAddress,HttpSession session) {

    FrontUserinfo f=(FrontUserinfo) session.getAttribute("FrontUserinfo");
    shippingAddress.setUserInfoId(f.getUserInfoId());
    frontUserAddressService.addAddress(shippingAddress);

    return "redirect:/front/userAddress";
}

    //修改地址
    @RequestMapping("/userAddressEdit_show")
    public String userAddressEdit_show(Model model,Integer shippingAddressId){
        ShippingAddress shippingAddress=frontUserAddressService.findByShippingAddressId(shippingAddressId);
        model.addAttribute("shippingAddress",shippingAddress);

        return "/front/udai_address_edit";

    }
    @RequestMapping("/userAddressEdit")
    public String userAddressEdit(ShippingAddress shippingAddress){
        frontUserAddressService.updateAddress(shippingAddress);
        return "redirect:/front/userAddress";
    }

    //删除地址
    @RequestMapping("/userAddressDelete")
    public String userAddressDelete(Integer shippingAddressId){
        frontUserAddressService.deleteAddress(shippingAddressId);
        return  "redirect:/front/userAddress";
    }


}
